package chapter03;

import java.util.ArrayList;

public class SingleQueueArray<E> {


    private int front;
    private int rear;
    private int maxSize;
    private ArrayList<E> elements;

    public SingleQueueArray() {
        this(100);
    }

    public SingleQueueArray(int size) {
        this.front = 0;
        this.rear = 0;
        this.maxSize = size;
        elements = new ArrayList<>(maxSize);
    }

    void enqueue(E e) {
        elements.add(e);
        if (!full()) {
            if (elements.size() < maxSize) {
                elements.add(e);
            }else {
                elements.set(rear, e);
            }
            rear = (rear + 1) % maxSize;
        }
    }

    E dequeue() {
        E temp = null;
        if (!isEmpty()) {
            temp = elements.get(front);
            front = (front + 1) % maxSize;
        }
        return temp;
    }

    boolean isEmpty() {
        return front == rear;
    }

    boolean full() {
        return (rear + 1) % maxSize == front;
    }
}
